package com.nutransit;

import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.mylibrary.mylibrary.imageloader.ImageLoader;
import com.mylibrary.mylibrary.parsing.AsyncTaskInterface;
import com.mylibrary.mylibrary.parsing.AsyncTaskUtility;
import com.mylibrary.mylibrary.util.CommonCode;
import com.mylibrary.mylibrary.util.SessionData;
import com.nutransit.commoncode.Webservice_parametrs;
import com.nutransit.service.MyService;

public class Fragment_Van_Root_Details extends Fragment implements Webservice_parametrs
{
	private View rootview;
	private ImageView iv_Profile_Pic, iv_Root_Image;
	private TextView tv_UserName, tv_Address, tv_Accept;
	
	private Context context;
	private Resources resources;
	private ImageLoader imageLoader;
	private SessionData sessionData;
	
	private String strVID = "3", strDriverId;
	private HashMap<String, String> hashMap;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		rootview = inflater.inflate(R.layout.fragment_root_details, container,false);
		init_var();
		
		get_data();
		return rootview;
	}

	private void init_var()
	{
		context = getActivity();
		resources = context.getResources();
		imageLoader = new ImageLoader(context);
		sessionData = new SessionData(context, PACKAGE_NAME);
		
		strVID = getArguments().getString(TAG_vid);
		
		rootview.findViewById(R.id.tv_Price).setVisibility(View.GONE);
		iv_Profile_Pic = (ImageView) rootview.findViewById(R.id.iv_Profile_Pic);
		iv_Root_Image = (ImageView) rootview.findViewById(R.id.iv_Root_Image);
		
		tv_UserName = (TextView) rootview.findViewById(R.id.tv_UserName);
		tv_Address = (TextView) rootview.findViewById(R.id.tv_Address);
		tv_Accept = (TextView) rootview.findViewById(R.id.tv_Accept);
		tv_Accept.setText(resources.getString(R.string.RESERVE));
		tv_Accept.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View arg0)
			{
				reserve_van();
			}
		});
		
	}
	
	private void get_data()
	{
		hashMap = new HashMap<String, String>();
		hashMap.put(TAG_vid, strVID);
		new AsyncTaskUtility(context, hashMap, URL_GET_NU_VAN_DRIVERS).getJSONResponse(new AsyncTaskInterface()
		{
			@Override
			public void getJSONObjectFromAsynkTask(JSONObject json)
			{
				try
				{
					if(json!=null)
					{
						if(json.has(TAG_message))
							CommonCode.showMessage(json.getString(TAG_message), context);
						if(json.getString(TAG_status).equalsIgnoreCase(SUCCESS))
						{
							JSONArray jsonArray_nuvandrivers = json.getJSONArray("nuvandrivers");
								
							if(jsonArray_nuvandrivers!=null && jsonArray_nuvandrivers.length()>0)
							{
								JSONObject jsonObject_nuvandrivers = jsonArray_nuvandrivers.getJSONObject(0);
								String str_DiverImgURL = json.getString("diverimgurl")+jsonObject_nuvandrivers.getString("photo");
								String str_VanRouteImgURL = json.getString("vanrouteimgurl")+jsonObject_nuvandrivers.getString("routeImage");
								tv_UserName.setText(jsonObject_nuvandrivers.getString("title"));
								tv_Address.setText(jsonObject_nuvandrivers.getString("license_id"));
								strDriverId = jsonObject_nuvandrivers.getString(TAG_id);
								imageLoader.DisplayImage(str_DiverImgURL, iv_Profile_Pic);
								imageLoader.DisplayImage(str_VanRouteImgURL, iv_Root_Image);
							}
							else
								rootview.findViewById(R.id.view_Request).setVisibility(View.GONE);
						}
						else
							rootview.findViewById(R.id.view_Request).setVisibility(View.GONE);
					}
				}
				catch (JSONException e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	protected void reserve_van()
	{
		try
		{
			hashMap = new HashMap<String, String>();
			hashMap.put(TAG_van, strVID);
			hashMap.put(TAG_driverid, strDriverId);
			hashMap.put(TAG_userid, MainActivity_Nutransit.jsonObject_data.getString(TAG_id));
			hashMap.put(TAG_price, "");
			hashMap.put(TAG_startpoint, "");
			hashMap.put(TAG_endpoint, "");
			hashMap.put(TAG_distance, "");
			
			
			new AsyncTaskUtility(context, hashMap, URL_RESERVE_NU_VAN_DRIVERS).getJSONResponse(new AsyncTaskInterface()
			{
				@Override
				public void getJSONObjectFromAsynkTask(JSONObject json)
				{
					try
					{
						if(json!=null)
						{
							if(json.has(TAG_message))
								CommonCode.showMessage(json.getString(TAG_message), context);
							if(json.getString(TAG_status).equalsIgnoreCase(SUCCESS))
							{
								JSONObject jsonObject = json.getJSONArray(TAG_data).getJSONObject(0);
								String strReservationId = jsonObject.getString(TAG_idprimary);
								if(!MainActivity_Nutransit.arrayList_ReservationIds.contains(strReservationId))
									MainActivity_Nutransit.arrayList_ReservationIds.add(strReservationId);
								sessionData.setString(TAG_reservation_ids, new Gson().toJson(MainActivity_Nutransit.arrayList_ReservationIds));
								if(!CommonCode.isMyServiceRunning(MyService.class, context))
								{
									context.stopService(new Intent(context, MyService.class));
									context.startService(new Intent(context, MyService.class));
								}
							}
						}
					}
					catch (JSONException e)
					{
						e.printStackTrace();
					}
				}
			});
		}
		catch (JSONException e)
		{
			e.printStackTrace();
		}
	}

}